package com.zmn.oms.zmn.business.impl.master;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zmn.base.plat.engine.common.constant.DubboConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.consts.GlobalConsts;
import com.zmn.engstock.dubbo.interfaces.stock.StockListRemoteService;
import com.zmn.oms.common.constant.OmsErrorCodeConsts;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.bo.master.quotation.MasterInfoBO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.DistributeMasterQuery;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.ZsOrderDistributeVO;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.master.MasterQuotationBService;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsDistributeWorkBService;
import javafx.util.Pair;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author sunlife
 * @date:2022/3/16 9:07 下午
 * description:
 */
@Service
public class MasterQuotationBServiceImpl implements MasterQuotationBService {

    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ZsDistributeWorkBService zsDistributeWorkBService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private StockListRemoteService stockListRemoteService;

    @Override
    public List<MasterInfoBO> listOnlineMasterByOrderIdAndWorkId(Long orderId, Long workId) throws OmsBaseException {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        if (null == orderWork) {
            throw new OmsBaseException(OmsErrorCodeConsts.ORDER_NOT_FOUND, "订单不存在");
        }
        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);

        DistributeMasterQuery query = zsDistributeWorkBService.buildDistributeMasterQuery(orderWork, orderDetail, false);
        query.setBackdoorStatus(GlobalConsts.YES);
        List<ZsOrderDistributeVO> masterList = zsDistributeWorkBService.listSpDistributeMasterByQuery(query);

        if (CollectionUtils.isEmpty(masterList)) {
            return Lists.newArrayList();
        }

        // 判断工程师是否空闲状态
        Set<Integer> masterIdSet = masterList.stream().map(ZsOrderDistributeVO::getMasterId).collect(Collectors.toSet());
        ResponseDTO<List<Pair<Integer, String>>> responseDTO = stockListRemoteService.listStockUsedOrderByEngineerSet(masterIdSet, null);

        Map<Integer, String> masterIdMap = Maps.newHashMap();
        if (responseDTO.isSuccess() && CollectionUtils.isNotEmpty(responseDTO.getData())) {
            masterIdMap = responseDTO.getData().stream().collect(Collectors.toMap(e -> (Integer)e.getKey(), e -> (String)e.getValue(), (e1, e2) -> e1));
        }

        List<MasterInfoBO> masterInfoList = Lists.newArrayListWithCapacity(masterList.size());
        for (ZsOrderDistributeVO distributeVO : masterList) {
            MasterInfoBO masterInfoBO = new MasterInfoBO();
            masterInfoBO.setMasterId(distributeVO.getMasterId());
            masterInfoBO.setMasterName(distributeVO.getMasterName());
            masterInfoBO.setAvatarSrc(distributeVO.getAvatarSrc());
            masterInfoBO.setMobile(distributeVO.getMasterPhone());

            String orderIdStr = masterIdMap.get(distributeVO.getMasterId());
            masterInfoBO.setFreeStatus(StringUtils.isBlank(orderIdStr) ? GlobalConsts.YES : GlobalConsts.NO);

            masterInfoList.add(masterInfoBO);
        }

        return masterInfoList;
    }
}
